package api

import (
	"GopherFocus/app/model"
	"GopherFocus/app/system/index/internal/define"
	"GopherFocus/app/system/index/internal/service"
	"GopherFocus/library/response"
	"github.com/gogf/gf/net/ghttp"
)

var Reply = replyApi{}

type replyApi struct{}

// 获取回复列表
func (a *replyApi) Index(r *ghttp.Request) {
	var (
		req *define.ReplyGetListReq
	)
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	// 获取回复列表
	if getListRes, err := service.Reply.GetList(r.Context(), req.ReplyGetListInput); err != nil {
		response.JsonExit(r, 1, err.Error())
	} else {
		service.View.RenderTpl(r, "index/reply.html", model.View{Data: getListRes})
	}
}

// 创建回复
func (a *replyApi) DoCreate(r *ghttp.Request) {
	var (
		req *define.ReplyDoCreateReq
	)
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	if err := service.Reply.Create(r.Context(), req.ReplyDoCreateInput); err != nil {
		response.JsonExit(r, 1, err.Error())
	} else {
		response.JsonExit(r, 0, "")
	}
}

// 删除回复
func (a *replyApi) DoDelete(r *ghttp.Request) {
	var (
		req *define.ReplyDoDeleteReq
	)
	if err := r.Parse(&req); err != nil {
		response.JsonExit(r, 1, err.Error())
	}
	if err := service.Reply.DoDelete(r.Context(), req.Id); err != nil {
		response.JsonExit(r, 1, err.Error())
	} else {
		response.JsonExit(r, 0, "")
	}
}
